package com.sun.study;

/**
 * @Author Sun
 * @Date 2020/4/12 15:29
 * @Version 1.0
 */
public class testReflection_02 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println(person.name);

        //方式一，通过Class类的静态方法forName()获得
        Class c1 = Class.forName("com.sun.study.Student");
        System.out.println(c1.hashCode());

        //方式二，通过类名获取，建议使用
        Class c2 = Student.class;
        System.out.println(c2.hashCode());

        //方式三，通过对象名获取
        Class c3 = person.getClass();
        System.out.println(c3.hashCode());

        //方式四，基本内置类型的包装类都有一个TYPE属性
        Class type = Integer.TYPE;

        //获得父类类型
        Class superclass = c1.getSuperclass();
        System.out.println(superclass);
    }
}

class Person{
    public String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person{
    public Student() {
        this.name = "学生";
    }
}

class Teacher extends Person{
    public Teacher() {
        this.name = "老师";
    }
}